package nio.filechannel;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 将文件中数据读取到缓冲区
 */
public class NIOFileChannel2 {
    public static void main(String[] args) throws IOException {
        // 创建文件输入流，获取对应channel
        FileInputStream fileInputStream = new FileInputStream("D:\\java-ee\\java-ee\\netty\\src\\test01.txt");
        FileChannel channel = fileInputStream.getChannel();
        // 创建缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate(2);
        while (true) {
            byteBuffer.clear();
            int read = channel.read(byteBuffer);
            if (read == -1) {
                break;
            }
            System.out.println(new String(byteBuffer.array(), 0, read));
        }
        fileInputStream.close();
    }
}
